<template> 
  <div>
    <el-upload
      class="upload-demo"
      ref="upload"
      action=""
      list-type="picture"
      :multiple="false" :show-file-list="showFileList"
      :file-list="fileList"
      :headers="token"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview"
      :auto-upload="false">
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
    </el-upload>
  </div>
</template>
<script>
  export default {
    name: 'singleUpload',
    props: {
      value: String,
      url: '',
      quality: '',
      modelName: ''
    },
    computed: {
      imageUrl() {
        return this.value;
      },
      urlPath(){
      let url = this.url+"?moduleName="+this.modelName+"&quality="+ this.quality;
      console.log(url)
      return url;
    },
      imageName() {
        if (this.value != null && this.value !== '') {
          return this.value.substr(this.value.lastIndexOf("/") + 1);
        } else {
          return null;
        }
      },
      fileList() {
        return [{
          name: this.imageName,
          url: this.imageUrl
        }]
      },
      showFileList: {
        get: function () {
          return this.value !== null && this.value !== ''&& this.value!==undefined;
        },
        set: function (newValue) {
        }
      }
    },created(){
      console.log(this.$http.headers())
    },
    data() {
      return {
        dialogVisible: false,
        token: this.$http.headers()
      };
    },
    methods: {
      emitInput(val) {
        this.$emit('input', val)
      },
      handleRemove(file, fileList) {
        this.emitInput('');
      },
      handlePreview(file) {
        this.dialogVisible = true;
      },

      handleUploadSuccess(res, file) {
        console.log("上传成功...")
        console.log(res)
        // this.showFileList = true;
        // this.fileList.pop();
        // this.fileList.push({name: file.name, url: this.dataObj.host + '/' + this.dataObj.key.replace("${filename}",file.name) });
        // this.emitInput(this.fileList[0].url);
      },
      submitUpload() {
        console.log(this.fileList)
      },
    }
  }
</script>
<style>

</style>


